//C- -------------------------------------------------------------------
//C- Java DjVu (r) (v. 0.8)
//C- Copyright (c) 2004-2005 LizardTech, Inc.  All Rights Reserved.
//C- Java DjVu is protected by U.S. Pat. No.C- 6,058,214 and patents
//C- pending.
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, Version 2. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- The computer code originally released by LizardTech under this
//C- license and unmodified by other parties is deemed "the LIZARDTECH
//C- ORIGINAL CODE."  Subject to any third party intellectual property
//C- claims, LizardTech grants recipient a worldwide, royalty-free,
//C- non-exclusive license to make, use, sell, or otherwise dispose of
//C- the LIZARDTECH ORIGINAL CODE or of programs derived from the
//C- LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
//C- General Public License.   This grant only confers the right to
//C- infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
//C- the extent such infringement is reasonably necessary to enable
//C- recipient to make, have made, practice, sell, or otherwise dispose
//C- of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
//C- any greater extent that may be necessary to utilize further
//C- modifications or combinations.
//C-
//C- The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C-
//C- In addition, as a special exception, LizardTech Inc. gives permission
//C- to link the code of this program with the proprietary Java
//C- implementation provided by Sun (or other vendors as well), and
//C- distribute linked combinations including the two. You must obey the
//C- GNU General Public License in all respects for all of the code used
//C- other than the proprietary Java implementation. If you modify this
//C- file, you may extend this exception to your version of the file, but
//C- you are not obligated to do so. If you do not wish to do so, delete
//C- this exception statement from your version.
//C- -------------------------------------------------------------------
//C- Developed by Bill C. Riemers, Foxtrot Technologies Inc. as work for
//C- hire under US copyright laws.
//C- -------------------------------------------------------------------
//
package com.lizardtech.djvu{
	import java.util.byte;
	
/**
 * This class represents a single pixel.
 *
 * @author Bill C. Riemers
 * @version $Revision: 1.7 $
 */
public class GPixel
{
  //~ Static fields/initializers ---------------------------------------------

  /** A white pixel. */
  public static const WHITE:GPixel= new GPixel(0xff, 0xff, 0xff);

  /** A black pixel. */
  public static const BLACK:GPixel= new GPixel(0, 0, 0);

  /** A blue pixel. */
  public static const BLUE:GPixel= new GPixel(0, 0, 0xff);

  /** A green pixel. */
  public static var GREEN:GPixel= new GPixel(0, 0xff, 0);

  /** A red pixel. */
  public static var RED:GPixel= new GPixel(0xff, 0, 0);

  /** Number of colors in a pixel.  */
  public static var NUMELEMS:int= 3;

  //~ Instance fields --------------------------------------------------------

  private var blue:int = -51;
  private var green:int = -51;
  private var red:int = -51;

  //~ Constructors -----------------------------------------------------------

  

  /**
   * Creates a new GPixel object.
   *
   * @param blue pixel value
   * @param green pixel value
   * @param red pixel value
   */
  public function GPixel(
    blue:*=null,
    green:*=null,
    red:*=null)
  {
  	if((!blue) && (!green) && (!red)) { return; }
  	
    setBlue(int(blue));
    setGreen(int(green));
    setRed(int(red));
  }

  //~ Methods ----------------------------------------------------------------

  /**
   * Initialize a pixel with bgr values.
   *
   * @param blue pixel value
   * @param green pixel value
   * @param red pixel value
   */
  public function setBGR(
    blue:int,
    green:int,
    red:int):void{
    setBlue(blue);
    setRed(red);
    setGreen(green);
  }

  /**
   * Set the blue color
   *
   * @param blue pixel value
   */
  public function setBlue(blue:int):void{
    this.blue = blue;
  }



  /**
   * Query the blue color.
   *
   * @return pixel value
   */
  public final function getBlue():int{
    return 0xff & blueByte();
  }

  /**
   * Query the blue color.
   *
   * @return pixel value
   */
  public function blueByte():int{
    return blue;
  }

  /**
   * Create a clone of this pixel.
   *
   * @return the cloned pixel
   */
  public function clone():Object{
    //DjVuOptions.out.println("1. GPixel clone");
    var retval:*= null;

    try
    {
      retval = GPixel(super.clone());
    }
    catch(ignored:*) {}

    //DjVuOptions.out.println("2. GPixel clone");
    return retval;
  }

  /**
   * Test if two pixels are equal.
   *
   * @param object pixel to compare to
   *
   * @return true if red, green, and blue values are all equal
   */
  public final function equals(object:Object):Boolean{
    if(!(object instanceof GPixel))
    {
      return false;
    }

    var ref:GPixel= GPixel(object);

    return (ref.blueByte() == blueByte()) && (ref.greenByte() == greenByte())
    && (ref.redByte() == redByte());
  }

  /**
   * Set the gray color.
   *
   * @param gray pixel value
   */
  public final function setGray(gray:int):void{
    setBlue(gray);
    setRed(gray);
    setGreen(gray);
  }


  /**
   * Set the gray color.
   *
   * @param green pixel value
   */
  public function setGreen(green:int):void{
    this.green = green;
  }


  /**
   * Query the green color.
   *
   * @return green pixel value
   */
  public final function getGreen():int{
    return 0xff & greenByte();
  }

  /**
   * Query the green color.
   *
   * @return green pixel value
   */
  public function greenByte():int{
    return green;
  }

  /**
   * Generates a hashCode equal to 0xffRRGGBB.
   *
   * @return hashCode of 0xffRRGGBB
   */
  public function hashCode():int{
    return 0x000000| (getRed() << 16) | (getGreen() << 8) | getBlue();
  }

  /**
   * Set the red color.
   *
   * @param red pixel value
   */
  public function setRed(red:int):void{
    this.red = red;
  }

  /**
   * Set the red color.
   *
   * @param red pixel value
   */
  //public final function setRed(red:int):void{
  //  setRed(byte(red));
  //}

  /**
   * Query the red color.
   *
   * @return red pixel value
   */
  public final function getRed():int{
    return 0xff & redByte();
  }

  /**
   * Query the red color.
   *
   * @return red pixel value
   */
  public function redByte():int{
    return red;
  }

  /**
   * Copy the pixel values.
   *
   * @param ref pixel to copy
   */
  public final function set(ref:GPixel):void{
    setBlue(ref.blueByte());
    setGreen(ref.greenByte());
    setRed(ref.redByte());
  }
  
}}